using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Risiko.Rendering
{
  /// <summary>
  /// This is a game component that implements IUpdateable.
  /// </summary>
  public abstract class AnimationRenderer : UIRenderer
  {
    protected Texture2D animationTexture, explosion;

    public AnimationRenderer(Game game)
      : base(game)
    {
      // TODO: Construct any child components here
    }

    /// <summary>
    /// Allows the game component to perform any initialization it needs to before starting
    /// to run.  This is where it can query for any required services and load content.
    /// </summary>
    public override void Initialize()
    {
      // TODO: Add your initialization code here

      base.Initialize();
    }

    protected override void LoadContent()
    {
      explosion = Game.Content.Load<Texture2D>(@"Images//Board//Explosion2");
      animationTexture = explosion;

      base.LoadContent();
    }

    public override void Update(GameTime gameTime)
    {
      base.Update(gameTime);
    }

    public override void UpdateAttack(GameTime gameTime)
    {
      // Check if the logic requested animations
      if (gameState.playerStates[(int)gameState.currentPlayer].MossaFatta)
      {
        //gameState.board.ActiveAnimations.Add(new State.Animation() { TotalTime = 1.5, TRef = gameState.playerStates[(int)gameState.currentPlayer].CurrAttack.to, HFrames = 5, VFrames = 5 });
        gameState.playerStates[(int)gameState.currentPlayer].MossaAnimata = false;
        gameState.playerStates[(int)gameState.currentPlayer].MossaFatta = false;
      }

      // Update animations
      if (!gameState.playerStates[(int)gameState.currentPlayer].MossaAnimata)
      {
        UpdateAnimations(gameTime);

        // If all the animations are finished, inform the logic
        if (gameState.board.ActiveAnimations.Count == 0)
          gameState.playerStates[(int)gameState.currentPlayer].MossaAnimata = true;
      }

      base.UpdateAttack(gameTime);
    }

    public override void UpdateStrategicMove(GameTime gameTime)
    {
      // Check if the logic requested animations
      if (gameState.playerStates[(int)gameState.currentPlayer].MossaFatta)
      {
        gameState.playerStates[(int)gameState.currentPlayer].MossaAnimata = false;
        gameState.playerStates[(int)gameState.currentPlayer].MossaFatta = false;
      }

      // Update animations
      if (!gameState.playerStates[(int)gameState.currentPlayer].MossaAnimata)
      {
        UpdateAnimations(gameTime);

        // If all the animations are finished, inform the logic
        if (gameState.board.ActiveAnimations.Count == 0)
          gameState.playerStates[(int)gameState.currentPlayer].MossaAnimata = true;
      }

      base.UpdateStrategicMove(gameTime);
    }

    private void UpdateAnimations(GameTime gameTime)
    {
      for (int i = 0; i < gameState.board.ActiveAnimations.Count; ++i)
      {
        var animation = gameState.board.ActiveAnimations[i];
        animation.CurrentTime += gameTime.ElapsedGameTime.TotalSeconds;
        if (animation.CurrentTime >= animation.TotalTime)
        {
          // If the animation is finished, remove it from the state
          gameState.board.ActiveAnimations.RemoveAt(i);
          i--;
          continue;
        }

        gameState.board.ActiveAnimations[i] = animation;
      }
    }

    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);
    }


    public override void DrawAttack(GameTime gameTime)
    {
      // Draw animations
      if (!gameState.playerStates[(int)gameState.currentPlayer].MossaAnimata)
        DrawAnimations(gameTime);

      base.DrawAttack(gameTime);
    }


    private void DrawAnimations(GameTime gameTime)
    {
      for (int i = 0; i < gameState.board.ActiveAnimations.Count; ++i)
      {
        var animation = gameState.board.ActiveAnimations[i];
        var index = (int)(animation.TotalTime / animation.CurrentTime);
        var frameWidth = explosion.Width / animation.HFrames;
        var frameHeight = explosion.Height / animation.VFrames;
        var territory = gameState.board.GetTerritory(animation.TRef);
        switch (animation.Type)
        {
          case State.AnimationType.Explosion:
            animationTexture = explosion;
            break;
        }
        spriteBatch.Draw(animationTexture, animation.Position, new Rectangle((index % animation.HFrames) * frameWidth, (index / animation.HFrames) * frameHeight, frameWidth, frameHeight), gameState.playerStates[(int)gameState.currentPlayer].color, 0.0f, new Vector2(frameWidth / 2, frameHeight / 2), 1.0f, SpriteEffects.None, 0.0f);
      }

    }
  }
}
